<template>
  <div class="sub-filter" v-if="filterData">
    <!-- 筛选区 -->
    <div class="item">
      <!-- 品牌独立渲染 -->
      <div class="head">品牌：</div>
      <div class="body">
        <a
          href="javascript:;"
          v-for="item in filterData.brands"
          :key="item.id"
          :class="{ active: item.id === filterData.brands.selected }"
           @click="changeBrand(item.id)"
        >
          {{ item.name }}
        </a>
      </div>
    </div>
    <div
      class="item"
      :key="index"
      v-for="(item, index) in filterData.saleProperties"
    >
      <!-- 品牌独立渲染 -->
      <div class="head">{{ item.name }}：</div>
      <div class="body">
        <a
          href="javascript:;"
          v-for="sub in item.properties"
          :key="sub.id"
          :class="{ active: sub.id === item.selected }"
          @click="changeSale(item, sub.id)"
        >
          {{ sub.name }}
        </a>
      </div>
    </div>
  </div>
</template>
<script>
import { ref, watch } from 'vue'
import { useRoute } from 'vue-router'
import { findSubCategoryFilter } from '@/api/category'
export default {
  name: 'SubFilter',
  setup (props, { emit }) {
    const filterData = ref(null)
    const route = useRoute()

    watch(() => route.params.id, async value => {
      if (!route.path.includes('/sub')) return
      const res = await findSubCategoryFilter(value)
      // console.log(res)
      res.result.brands.selected = null // 默认选中的品牌id
      res.result.brands.unshift({ id: null, name: '全部' })
      res.result.saleProperties.forEach(item => {
        item.properties.selected = null // 默认销售数据高亮
        item.properties.unshift({ id: null, name: '全部' })
      })
      filterData.value = res.result
    },
    {
      immediate: true
    }
    )

    const getSelectedFilter = () => {
      // 获取所有筛选的条件
      const filters = {
        brandId: null,
        attrs: []
      }
      // 根据filterData的selected属性去获取
      const brandId = filterData.value.brands.selected
      if (brandId) {
        filters.brandId = brandId
      }
      filterData.value.saleProperties.forEach(item => {
        if (item.selected) {
          filters.attrs.push({
            groupName: item.name,
            propertyName: item.properties.find(v => v.id === item.selected).name
          })
        }
      })

      return filters
    }

    // 选择品牌
    const changeBrand = (id) => {
      filterData.value.brands.selected = id
      emit('changeFilter', getSelectedFilter())
    }

    // 选择属性
    const changeSale = (item, id) => {
      item.selected = id
      // 需要把数据传给父组件
      emit('changeFilter', getSelectedFilter())
    }

    return {
      filterData,
      changeBrand,
      changeSale,
      getSelectedFilter
    }
  }
}
</script>
<style scoped lang='less'>
  // 筛选区
  .sub-filter {
    background: #fff;
    padding: 25px;
    .item {
      display: flex;
      line-height: 40px;
      .head {
        width: 80px;
        color: #999;
      }
      .body {
        flex: 1;
        a {
          margin-right: 36px;
          transition: all .3s;
          display: inline-block;
          &.active,
          &:hover {
            color: @xtxColor;
          }
        }
      }
    }
  }
</style>
